from django.db import models
from user_app.models import User
from subjects_app.models import Subject
from content_app.models import Content

class LearningProgress(models.Model):
    # 用户外键
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='learning_progresses', verbose_name='用户')
    
    # 内容类型和内容ID，使用ContentType可以实现多态关联
    CONTENT_TYPES = (
        ('subject', '学科'),
        ('content', '学习内容'),
        ('question', '试题'),
        ('chapter', '章节'),
    )
    content_type = models.CharField(max_length=20, choices=CONTENT_TYPES, verbose_name='内容类型')
    content_id = models.IntegerField(verbose_name='内容ID')
    
    # 学习进度信息
    progress_percentage = models.IntegerField(default=0, verbose_name='完成百分比')
    is_completed = models.BooleanField(default=False, verbose_name='是否完成')
    learning_time_seconds = models.IntegerField(default=0, verbose_name='学习时长(秒)')
    last_learning_time = models.DateTimeField(auto_now=True, verbose_name='最后学习时间')
    
    # 元数据
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '学习进度'
        verbose_name_plural = verbose_name
        db_table = 't_learning_progress'
        # 确保一个用户对一个内容只有一条进度记录
        unique_together = ('user', 'content_type', 'content_id')

# 学习历史记录模型，可以记录更详细的学习过程
class LearningHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='learning_histories', verbose_name='用户')
    content_type = models.CharField(max_length=20, choices=LearningProgress.CONTENT_TYPES, verbose_name='内容类型')
    content_id = models.IntegerField(verbose_name='内容ID')
    start_time = models.DateTimeField(verbose_name='开始学习时间')
    end_time = models.DateTimeField(verbose_name='结束学习时间')
    duration_seconds = models.IntegerField(verbose_name='学习时长(秒)')
    
    class Meta:
        verbose_name = '学习历史'
        verbose_name_plural = verbose_name
        db_table = 't_learning_history'
